package com.yunhai.web.controller.reveal;

import java.util.List;
import java.util.Arrays;

import com.yunhai.common.annotation.Log;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.enums.BusinessType;
import com.yunhai.common.utils.poi.ExcelUtil;
import com.yunhai.system.domain.ProductType;
import com.yunhai.system.service.ProductTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 产品类型Controller
 *
 * @author jxj
 * @date 2021-01-15
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/reveal/productType")
public class ProductTypeController extends BaseController {

    private final ProductTypeService productTypeService;

    /**
     * 查询产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductType productType) {
        startPage();
        List<ProductType> list = productTypeService.queryList(productType);
        return getDataTable(list);
    }

    /**
     * 导出产品类型列表
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:export')" )
    @Log(title = "产品类型" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ProductType productType) {
        List<ProductType> list = productTypeService.queryList(productType);
        ExcelUtil<ProductType> util = new ExcelUtil<ProductType>(ProductType.class);
        return util.exportExcel(list, "productType" );
    }

    /**
     * 获取产品类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(productTypeService.getById(id));
    }

    /**
     * 新增产品类型
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:add')" )
    @Log(title = "产品类型" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductType productType) {
        return toAjax(productTypeService.save(productType) ? 1 : 0);
    }

    /**
     * 修改产品类型
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:edit')" )
    @Log(title = "产品类型" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductType productType) {
        return toAjax(productTypeService.updateById(productType) ? 1 : 0);
    }

    /**
     * 删除产品类型
     */
    @PreAuthorize("@ss.hasPermi('reveal:productType:remove')" )
    @Log(title = "产品类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
